package com.example.ccj.b.c.custom.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.image.MLocalMedia;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;

import java.util.List;


/**
 * 图片加载
 */
public class AdpterForRecyclerImagesLoadAndRemoveButton extends RecyclerView.Adapter<AdpterForRecyclerImagesLoadAndRemoveButton.LinearViewHolder>{

    private final Context mContext;
    private final OnItemClickListener mListrner;
    private final List<MLocalMedia> imagesArray;
    private OnItemDeleteListener deleteListener;
    private boolean isCanDelete = false;

    //构造方法
    public AdpterForRecyclerImagesLoadAndRemoveButton(Context context,
                                                      List<MLocalMedia> images,
                                                      boolean isCanDelete,
                                                      OnItemClickListener clickListrner,
                                                      OnItemDeleteListener deleteListener){
        this.mContext = context;
        this.isCanDelete = isCanDelete;
        this.imagesArray = images;
        this.mListrner = clickListrner;
        this.deleteListener = deleteListener;
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    @NonNull
    @Override
    public AdpterForRecyclerImagesLoadAndRemoveButton.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdpterForRecyclerImagesLoadAndRemoveButton
                .LinearViewHolder(LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.custom_iamges_add_and_remove, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AdpterForRecyclerImagesLoadAndRemoveButton.LinearViewHolder holder,  int position) {
        MLocalMedia mLocalMedia = imagesArray.get(position);

        if (isGoneDeleteIcon) {
            holder.mImageDelete.setVisibility(View.GONE);
        } else {
            holder.mImageDelete.setVisibility(View.VISIBLE);
        }

        LogUtils.d("加载图片：" + mLocalMedia.getAvailablePath());
        ImageUtils.loadImage(mContext,mLocalMedia.getAvailablePath(),holder.mImageView);
//        ImageUtils.loadImage(mContext,mLocalMedia.getRealPath(),holder.mImageView);

        final int po = position;
//        if (imagesArray.get(po).uri != null && StringUtils.isNoEmpty(imagesArray.get(po).uri.getPath())) {
//            ImageUtils.loadImage(mContext,imagesArray.get(po).uri,holder.mImageView);
////            Glide.with(mContext).load(imagesArray.get(position).uri).into(holder.mImageView);
//        } else {
//            if (imagesArray.get(po).imageType == 0) {
//                ImageUtils.loadImage(mContext,imagesArray.get(po).uri,holder.mImageView);
////                Glide.with(mContext).load(imagesArray.get(position).uri).into(holder.mImageView);
//            } else if (imagesArray.get(po).imageType == 1){
//                ImageUtils.loadOSSImageThumbnail(mContext,imagesArray.get(po).urlPath,holder.mImageView);
////                Glide.with(mContext).load(imagesArray.get(position).urlPath + Config.LOAD_PICTRUE_URL_SUFFIX).placeholder(R.mipmap.loade).error(R.mipmap.error_network_image).into(holder.mImageView);
//            } else if (imagesArray.get(po).imageType == 2) {
//                holder.mImageView.setImageResource(R.mipmap.def_error_network_loading);
//            } else if (imagesArray.get(po).imageType == 3) {
//                holder.mImageView.setImageResource(imagesArray.get(po).iamgeId);
//            }
//        }

        if (isCanDelete) {
            holder.mImageDelete.setVisibility(View.VISIBLE);
            //删除图片事件
            holder.mImageDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (deleteListener != null) {
//                    MyImages myImages = imagesArray.get(position);
                        deleteListener.onItemsClick(imagesArray,imagesArray.get(po));
                        removeData(po);
                    }else{
                        ToastUtils.showShort("删除失败，未设置删除回调事件");
                    }
                }
            });

        } else {
            holder.mImageDelete.setVisibility(View.GONE);
        }

        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListrner != null) {
                    mListrner.onItemsClick(po);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imagesArray.size();
    }

    public static class LinearViewHolder extends  RecyclerView.ViewHolder{
        private ImageView mImageView;
        private ImageView mImageDelete;
        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.images_and_remove_images);
            mImageDelete = itemView.findViewById(R.id.images_and_remove_remove);
        }
    }

    //  删除item
    public void removeData(int position) {
        imagesArray.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    /**
     * 设置图片尺寸
     * @param size
     */
    public void setImageSize(int size){

    }

    boolean isGoneDeleteIcon = false;
    /**
     * 隐藏删除按钮
     */
    public void goneDeleteIcon(boolean isGone){
        isGoneDeleteIcon = isGone;
    }

    /**
     * Item点击事件回调
     */
    public interface OnItemClickListener{
        void onItemsClick(int po);
    }

    /**
     * Item点击事件回调
     */
    public interface OnItemDeleteListener{
        void onItemsClick(List<MLocalMedia> myImages, MLocalMedia deleteImage);
    }
}
